package myapps.ttclient.activity.ppt;

import java.util.ArrayList;

import myapps.ttclient.GlobalVariables;
import myapps.ttclient.Helper;
import myapps.ttclient.R;
import myapps.ttclient.adapter.PPTSelectActiveWindowArrayAdapter;
import myapps.ttclient.app.TTClientApplication;
import myapps.ttclient.baseclass_and_interface.TTClientMessage;
import myapps.ttclient.baseclass_and_interface.TTClientMessageReceiver;
import myapps.ttclient.protocol.message.powerpoint.PPTActivateWindowRequestMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTWindowsRelationshipRequestMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTWindowsRelationshipResponseMessage;
import myapps.ttclient.struct.PresentationWindowPropertiesStruct;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class PPTSelectActiveWindowActivity extends Activity 
		implements OnItemClickListener, TTClientMessageReceiver {

	private TTClientApplication application;

	private ListView listView;

	private ArrayList<PresentationWindowPropertiesStruct> arrayList;
	private ArrayAdapter<PresentationWindowPropertiesStruct> adapter;

	private Button btnRefresh;
	private Button btnApply;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		Helper.updateFullscreenStatus(this, true);
		
		//Helper.keepScreenOn(this);
		
		this.application = (TTClientApplication) this.getApplication();

		super.onCreate(savedInstanceState);

		setContentView(R.layout.select_active_window);

		listView = (ListView) findViewById(R.id.listview_select_active_window);

		this.arrayList = new ArrayList<PresentationWindowPropertiesStruct>();

		this.adapter = new PPTSelectActiveWindowArrayAdapter(this, 
				R.layout.select_active_window_rowlayout, this.arrayList);

		this.listView.setAdapter(this.adapter);

		this.listView.setOnItemClickListener(this);
		
		btnRefresh = (Button) findViewById(R.id.btnRefresh);
		btnRefresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				PPTSelectActiveWindowActivity.this.application.sendMessage(
						new PPTWindowsRelationshipRequestMessage(GlobalVariables.getTemporaryKey()));
				
			}
		});

		btnApply = (Button) findViewById(R.id.btnApply);
		btnApply.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = currentActiveWindowIndex;

				if(0 <= index && index < arrayList.size()) {
					String name = arrayList.get(index).presentationName;

					PPTSelectActiveWindowActivity.this.application.sendMessage(
							new PPTActivateWindowRequestMessage(GlobalVariables.getTemporaryKey(),
									index, name));
				} else {
					Helper.ShowNoticeDialog(PPTSelectActiveWindowActivity.this, "Notice Dialog", "Invalid Opearation");
				}
			}
		});

		this.application.sendMessage(new PPTWindowsRelationshipRequestMessage(GlobalVariables.getTemporaryKey()));
	}

	private int currentActiveWindowIndex = -1;

	@Override
	protected void onResume() {
		this.application.registerMessageReceiver(this);
		super.onResume();
	}

	@Override
	protected void onPause() {
		this.application.unregisterMessageReceiver(this);
		super.onPause();
	}

	@Override
	public void receiveMessage(TTClientMessage message) {
		if(message instanceof PPTWindowsRelationshipResponseMessage) {
			PPTWindowsRelationshipResponseMessage msg = (PPTWindowsRelationshipResponseMessage) message;
			String[] presentationNames = msg.presentationNames;
			int activeWindowIndex = msg.activeWindowIndex;

			if(presentationNames != null && activeWindowIndex >= 0) {
				currentActiveWindowIndex = activeWindowIndex;

				this.arrayList.clear();

				int n = presentationNames.length;

				for(int i = 0; i < n; i++) {
					PresentationWindowPropertiesStruct props = new PresentationWindowPropertiesStruct();

					props.presentationName = presentationNames[i];

					if(i == activeWindowIndex) {
						props.isActive = true;
					} else {
						props.isActive = false;
					}

					this.arrayList.add(props);
				}

				this.runOnUiThread(new Runnable() {
					public void run() {
						PPTSelectActiveWindowActivity.this.adapter.notifyDataSetChanged();
					}
				});
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		for(int i = 0; i < this.arrayList.size(); i++) {
			this.arrayList.get(i).isActive = false;
		}

		this.arrayList.get(position).isActive = true;

		currentActiveWindowIndex = position;

		this.adapter.notifyDataSetChanged();
	}
}